package com.fangcun.controller.admin.old;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProAffairException;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseAdminController;
import com.fangcun.entity.AdminMenuPermission;
import com.fangcun.entity.AdminRole;
import com.fangcun.entity.AdminRolePermission;
import com.fangcun.entity.AdminUserRole;
import com.fangcun.service.admin.PermissionService;

/**
 * @作者： 陳曉鬆 @时间： 2020年5月9日 - 下午1:57:22 @description：->
 */
@RestController
@RequestMapping("/admin/{version}/auth/permission")
public class PermissionController extends BaseAdminController {

	private static Logger LOG = LoggerFactory.getLogger(PermissionController.class);

	@Resource(name = "permissionService")
	private PermissionService permissionService;

	/** 查询出菜单权限列表 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/all/list")
	private ResponseMsg addList(@RequestBody AdminMenuPermission menu) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				msg.setData(permissionService.getMenuPermissionList(menu));
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProException e) {
			LOG.error("-权限-查询出菜单权限列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-查询出菜单权限列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 增加菜单权限 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "menu/add")
	private ResponseMsg menuadd(@RequestBody AdminMenuPermission menu) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				msg.setData(permissionService.insertMenuPermission(menu));
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-增加菜单权限->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-增加菜单权限->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改菜单权限 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "menu/update")
	private ResponseMsg menuupdate(@RequestBody AdminMenuPermission menu) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				permissionService.updateMenuPermission(menu);
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-修改菜单权限->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-修改菜单权限->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 删除菜单权限 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "menu/delete")
	private ResponseMsg menudelete(Long menuId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				permissionService.deleteMenuPermission(menuId);
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-删除菜单权限->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-删除菜单权限->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 角色列表 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "role/list")
	private ResponseMsg rolelist(@RequestBody AdminRole role) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				msg.setData(permissionService.getRoleList(role));
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-角色列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-角色列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 添加角色 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "role/add")
	private ResponseMsg roleadd(@RequestBody AdminRole role) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				msg.setData(permissionService.insertRole(role));
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-添加角色->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-添加角色->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改角色 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "role/update")
	private ResponseMsg roleupdate(@RequestBody AdminRole role) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				permissionService.updateRole(role);
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-修改角色->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-修改角色->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 删除角色 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "role/delete")
	private ResponseMsg roledelete(Long roleId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				permissionService.deleteRole(roleId);
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-删除角色->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-删除角色->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 修改用户的角色 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "roleuser/update")
	private ResponseMsg roleuserupdate(@RequestBody AdminUserRole role) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() == null) {// 只能超级管理员访问
				permissionService.updateUserRole(role);
			} else {
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
		} catch (ProAffairException e) {
			LOG.error("-权限-修改用户的角色->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-修改用户的角色->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 获取角色拥有权限 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "rolepermission/list")
	private ResponseMsg rolepermissionlist(Long roleId) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(permissionService.getRolePermissionList(roleId));
		} catch (ProAffairException e) {
			LOG.error("-权限-获取角色拥有权限->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-获取角色拥有权限->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 添加角色权限 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "rolepermission/add")
	private ResponseMsg rolepermissionadd(@RequestBody List<AdminRolePermission> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (getAdminParamUserId() != null) {// 只能超级管理员访问
				throw new ProValiDataException(ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnCode(), ErrCode.ERR_INVALIDSECRET_NO_EXISTED.getReturnMsg());
			}
			permissionService.insertRolePermission(request);
		} catch (ProAffairException e) {
			LOG.error("-权限-添加角色权限->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-权限-添加角色权限->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

}
